package com.gimi.cloud.jimi.client.service.userInfo.impl;

import com.gimi.cloud.jimi.client.service.userInfo.RdFaceInfoService;
import com.gimi.cloud.jimi.common.utils.DateUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.dao.dao.userInfo.RdFaceInfoDao;
import com.gimi.cloud.jimi.dto.userInfo.RdFaceInfoDTO;
import com.gimi.cloud.jimi.entity.userInfo.RdFaceInfo;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RdFaceInfoServiceImpl implements RdFaceInfoService {
    @Autowired
    private RdFaceInfoDao rdFaceInfoDao;

    @Override
    public void saveFaceInfo(RdFaceInfoDTO dto) {
        RdFaceInfo faceInfo = new RdFaceInfo();
        BeanUtils.copyProperties(dto, faceInfo);
        //TODO 定义有效期
		try {
			faceInfo.setReportEffectionEnd(DateUtil.strtoDate("2099-01-01","yyyy-MM-dd"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if(faceInfo.getfId() != null && faceInfo.getfId().intValue() > 0) {
            rdFaceInfoDao.updateByPrimaryKey(faceInfo);
        } else {
            rdFaceInfoDao.insert(faceInfo);
        }
    }

    @Override
    public RdFaceInfoDTO getFaceInfoByUser(Integer userId) {
		RdFaceInfoDTO result = rdFaceInfoDao.getFaceInfoByUser(userId);
		if(result != null) {
			result.setImage(AliOSSUtil.getFileUrl(result.getImage(), AliOSSUtil.getNoEffictDateByDay(1)));
		}
        return result;
    }

	@Override
	public Map<String, Object> getAuthenticationInfo(Integer userId) {
		Map<String, Object> map = new HashMap<>();
		RdFaceInfoDTO infoDTO = rdFaceInfoDao.getFaceInfoByUser(userId);
		//无数据
		if (infoDTO == null) {
			map.put("status", 0);
			return map;
		}
		RdFaceInfoDTO effectionDTO = rdFaceInfoDao.isEffection(userId);
		effectionDTO.setImage(AliOSSUtil.getFileUrl(effectionDTO.getImage(), AliOSSUtil.getNoEffictDateByDay(1)));
		//有数据未过期
		if (infoDTO != null && effectionDTO!= null ) {
			map.put("status", 1);
			map.put("data", effectionDTO);
			return map;
		}else {
			map.put("status", 2);
			return map;
		}
	}
}
